package com.cjx.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
public class SetData {
	
	/**
	 * 新增一轮
	 * @param map
	 */
	public void setRoundinfo(HashMap map){
		//数据库连接
		GetConnection conn = new GetConnection();
		Connection con = conn.getConn();
		
		
		List<HashMap> list = null;
		String sql = "select max(pk) as maxpk from roundinfo";
		list = conn.query(con, sql);	
		//roundinfo表当前最大PK
		
		int maxpk = 0;
		if(list.get(0).get("MAXPK")!=null && !"".equals(list.get(0).get("MAXPK"))){
			String temp_maxpk = list.get(0).get("MAXPK").toString();
			maxpk = Integer.parseInt(temp_maxpk)+1;
		}else{
			maxpk++;
		}
		
		//获取表单的输入值;
		String playera = map.get("playera")+"";
		String playerb = map.get("playerb")+"";
		String playerc = map.get("playerc")+"";
		String score1 = map.get("score1")+"";
		String score2 = map.get("score2")+"";
		String score3 = map.get("score3")+"";
				
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();// 取当前日期。
		System.out.println("Today is:" + format.format(cal.getTime()));
		
		String insert_sql = "insert into roundinfo values("+
				maxpk+","
				+maxpk+",'"
				+playera+"','"
				+playerb+"','"
				+playerc+"',"
				+score1+","
				+score2+","
				+score3+",'"
				+format.format(cal.getTime())+"',"
				+0+","
				+0+","
				+0+","
				+"'未结算'"+")";
		
		//插入数据库
		conn = new GetConnection();
		con = conn.getConn();
		conn.execute(con, insert_sql);
	}
	/**
	 * 新增一局
	 * @param map
	 */
	public void setGroupinfo(HashMap map){
		//数据库连接
		GetConnection conn = new GetConnection();
		Connection con = conn.getConn();
		
		
		List<HashMap> list = null;
		String sql = "select max(pk) as maxpk from groupinfo";
		list = conn.query(con, sql);	
		//roundinfo表当前最大PK
		int maxpk = 0;
		if(list.get(0).get("MAXPK")!=null && !"".equals(list.get(0).get("MAXPK"))){
			String temp_maxpk = list.get(0).get("MAXPK").toString();
			maxpk = Integer.parseInt(temp_maxpk)+1;
		}else{
			maxpk++;
		}
		
		//获取表单的输入值;
		String roundid = map.get("roundid")+"";
		String landlord = map.get("landlord")+"";
		String winner = map.get("winner")+"";
		String score = map.get("score")+"";
		String bomb = map.get("bomb")+"";
		int landlordscore = 0;
		if("地主赢".equals(winner)){
			if(Integer.parseInt(bomb)!=0){
				landlordscore = (int) (Integer.parseInt(score)*2*Math.pow(2, Integer.parseInt(bomb)));
			}else{
				landlordscore = Integer.parseInt(score);
			}
		}else if("农民赢".equals(winner)){
			if(Integer.parseInt(bomb)!=0){
				landlordscore = (int) (Integer.parseInt(score)*2*Math.pow(-2, Integer.parseInt(bomb)));
			}else{
				landlordscore = -Integer.parseInt(score);
			}
		}
		
	
		String insert_sql = "insert into groupinfo values("+
				maxpk+",'"
				+roundid+"','"
				+landlord+"','"
				+winner+"','"
				+score+"',"
				+bomb+",'"
				+landlordscore+"')";
		
		//插入数据库
		conn = new GetConnection();
		con = conn.getConn();
		conn.execute(con, insert_sql);
	}
	/**
	 * 结算一轮
	 */
	public void setStatistics(String roundid){
		//数据库连接
		GetConnection conn = new GetConnection();
		Connection con = conn.getConn();
		
		List<HashMap> list_roundinfo = null;
		List<HashMap> list_mappk = null;
		List<HashMap> list_query = null;
		GetData gd = new GetData();
		list_roundinfo = gd.getRoundinfo(roundid);
		int scorea=0;
		int scoreb=0;
		int scorec=0;
		
		
		//玩家A数据
		String sql_query_persona_l = "select * from groupinfo where roundid="+roundid+" and landlord='"+list_roundinfo.get(0).get("PERSONA")+"' and winner='地主赢'";
		String sql_query_persona_f = "select * from groupinfo where roundid="+roundid+" and landlord='"+list_roundinfo.get(0).get("PERSONA")+"' and winner='农民赢'";
		int persona_groups = 0;
		//数据库连接
		conn = new GetConnection();
		con = conn.getConn();
		list_query = conn.query(con, sql_query_persona_l);
		persona_groups+=list_query.size();
		int persona_score_l = 0;
		for(int i=0;i<list_query.size();i++){
			int score_temp = Integer.parseInt(list_query.get(i).get("SCORE").toString());
			int bomb_temp = Integer.parseInt(list_query.get(i).get("BOMB").toString());
			if(bomb_temp!=0){
				persona_score_l=(int)(score_temp*2*(Math.pow(2, bomb_temp)));
			}else{
				persona_score_l=score_temp;
			}
		}
		//数据库连接
		conn = new GetConnection();
		con = conn.getConn();
		list_query.clear();
		list_query = conn.query(con, sql_query_persona_f);
		persona_groups+=list_query.size();
		int persona_score_f = 0;
		for(int i=0;i<list_query.size();i++){
			int score_temp = Integer.parseInt(list_query.get(i).get("SCORE").toString());
			int bomb_temp = Integer.parseInt(list_query.get(i).get("BOMB").toString());
			if(bomb_temp!=0){
				persona_score_f=(int)(score_temp*2*(Math.pow(2, bomb_temp)));
			}else{
				persona_score_f=score_temp;
			}
		}
		int persona_score = persona_score_l-persona_score_f;
		if(persona_score!=0){
			scorea+=persona_score;
			scoreb+=(-persona_score/2);
			scorec+=(-persona_score/2);
		}
		
		
		//玩家B数据
		String sql_query_personb_l = "select * from groupinfo where roundid="+roundid+" and landlord='"+list_roundinfo.get(0).get("PERSONB")+"' and winner='地主赢'";
		String sql_query_personb_f = "select * from groupinfo where roundid="+roundid+" and landlord='"+list_roundinfo.get(0).get("PERSONB")+"' and winner='农民赢'";
		int personb_groups = 0;
		//数据库连接
		conn = new GetConnection();
		con = conn.getConn();
		list_query.clear();
		list_query = conn.query(con, sql_query_personb_l);
		personb_groups+=list_query.size();
		int personb_score_l = 0;
		for(int i=0;i<list_query.size();i++){
			int score_temp = Integer.parseInt(list_query.get(i).get("SCORE").toString());
			int bomb_temp = Integer.parseInt(list_query.get(i).get("BOMB").toString());
			if(bomb_temp!=0){
				personb_score_l=(int)(score_temp*2*(Math.pow(2, bomb_temp)));
			}else{
				personb_score_l=score_temp;
			}
		}
		//数据库连接
		conn = new GetConnection();
		con = conn.getConn();
		list_query.clear();
		list_query = conn.query(con, sql_query_personb_f);
		personb_groups+=list_query.size();
		int personb_score_f = 0;
		for(int i=0;i<list_query.size();i++){
			int score_temp = Integer.parseInt(list_query.get(i).get("SCORE").toString());
			int bomb_temp = Integer.parseInt(list_query.get(i).get("BOMB").toString());
			if(bomb_temp!=0){
				personb_score_f=(int)(score_temp*2*(Math.pow(2, bomb_temp)));
			}else{
				personb_score_f=score_temp;
			}
		}
		int personb_score = personb_score_l-personb_score_f;
		if(personb_score!=0){
			scoreb+=personb_score;
			scorea+=(-personb_score/2);
			scorec+=(-personb_score/2);
		}
		
		//玩家C数据
		String sql_query_personc_l = "select * from groupinfo where roundid="+roundid+" and landlord='"+list_roundinfo.get(0).get("PERSONC")+"' and winner='地主赢'";
		String sql_query_personc_f = "select * from groupinfo where roundid="+roundid+" and landlord='"+list_roundinfo.get(0).get("PERSONC")+"' and winner='农民赢'";
		int personc_groups = 0;
		//数据库连接
		conn = new GetConnection();
		con = conn.getConn();
		list_query.clear();
		list_query = conn.query(con, sql_query_personc_l);
		personc_groups+=list_query.size();
		int personc_score_l = 0;
		for(int i=0;i<list_query.size();i++){
			int score_temp = Integer.parseInt(list_query.get(i).get("SCORE").toString());
			int bomb_temp = Integer.parseInt(list_query.get(i).get("BOMB").toString());
			if(bomb_temp!=0){
				personc_score_l=(int)(score_temp*2*(Math.pow(2, bomb_temp)));
			}else{
				personc_score_l=score_temp;
			}
		}
		//数据库连接
		conn = new GetConnection();
		con = conn.getConn();
		list_query.clear();
		list_query = conn.query(con, sql_query_personc_f);
		personc_groups+=list_query.size();
		int personc_score_f = 0;
		for(int i=0;i<list_query.size();i++){
			int score_temp = Integer.parseInt(list_query.get(i).get("SCORE").toString());
			int bomb_temp = Integer.parseInt(list_query.get(i).get("BOMB").toString());
			if(bomb_temp!=0){
				personc_score_f=(int)(score_temp*2*(Math.pow(2, bomb_temp)));
			}else{
				personc_score_f=score_temp;
			}
		}
		int personc_score = personc_score_l-personc_score_f;
		if(personc_score!=0){
			scorec+=personc_score;
			scorea+=(-personc_score/2);
			scoreb+=(-personc_score/2);
		}
		
		
		//玩家A-roundinfo表当前最大PK
		conn = new GetConnection();
		con = conn.getConn();
		String sql = "select max(pk) as maxpk from statisticss";
		list_mappk = conn.query(con, sql);
		int maxpk = 0;
		if(list_mappk.get(0).get("MAXPK")!=null && !"".equals(list_mappk.get(0).get("MAXPK"))){
			String temp_maxpk = list_mappk.get(0).get("MAXPK").toString();
			maxpk = Integer.parseInt(temp_maxpk)+1;
		}else{
			maxpk++;
		}
		String sql_insert_persona = "insert into statisticss values("+maxpk+","+roundid+",'"+list_roundinfo.get(0).get("PERSONA")+"','"+scorea+"','"+persona_groups+"')";
		//数据库连接
		conn = new GetConnection();
		con = conn.getConn();
		conn.execute(con, sql_insert_persona);	
		
		
		//玩家B-roundinfo表当前最大PK
		conn = new GetConnection();
		con = conn.getConn();
		list_mappk = conn.query(con, sql);
		if(list_mappk.get(0).get("MAXPK")!=null && !"".equals(list_mappk.get(0).get("MAXPK"))){
			String temp_maxpk = list_mappk.get(0).get("MAXPK").toString();
			maxpk = Integer.parseInt(temp_maxpk)+1;
		}else{
			maxpk++;
		}
		String sql_insert_personb = "insert into statisticss values("+maxpk+","+roundid+",'"+list_roundinfo.get(0).get("PERSONB")+"','"+scoreb+"','"+personb_groups+"')";
		//数据库连接
		conn = new GetConnection();
		con = conn.getConn();
		conn.execute(con, sql_insert_personb);	
		
		
		//玩家C-roundinfo表当前最大PK
		//数据库连接
		conn = new GetConnection();
		con = conn.getConn();
		list_mappk = conn.query(con, sql);
		if(list_mappk.get(0).get("MAXPK")!=null && !"".equals(list_mappk.get(0).get("MAXPK"))){
			String temp_maxpk = list_mappk.get(0).get("MAXPK").toString();
			maxpk = Integer.parseInt(temp_maxpk)+1;
		}else{
			maxpk++;
		}
		String sql_insert_personc = "insert into statisticss values("+maxpk+","+roundid+",'"+list_roundinfo.get(0).get("PERSONC")+"','"+scorec+"','"+personc_groups+"')";
		conn = new GetConnection();
		con = conn.getConn();
		conn.execute(con, sql_insert_personc);	
		
		String sql_update_roundinfo = " update roundinfo set ascore="+scorea+",bscore="+scoreb+",cscore="+scorec+",status='已结算'"+" where roundid='"+roundid+"'";
		conn = new GetConnection();
		con = conn.getConn();
		conn.execute(con, sql_update_roundinfo);	
	}
	
}
